package com.cyy.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cyy.demo.entity.Module;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * rest接口接口调用
 */
@RestController
@RequestMapping("/rest")
public class RestfulController {

    /**
     * 接口地址
     */
    private String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13227875672";


    private String weatherUrl ="https://www.apiopen.top/weatherApi?city=";

    private String imgUrl= "http://pic37.nipic.com/20140113/8800276_184927469000_2.png";

    @Autowired
    public RestTemplate restTemplate;


    @GetMapping("/get1")
    public void queryKuaiDiNo(){
        ResponseEntity<String> object = restTemplate.getForEntity(url, String.class);
        System.out.println(object.getStatusCode());
        System.out.println(object.getStatusCodeValue());
        String body = object.getBody();
    }

    @GetMapping("/get2")
    public void queryKuaiDiNo2(){
        Object object = restTemplate.getForObject(url, String.class);
        object.toString();
    }

    @GetMapping("/get/{city}")
    public JSONObject getWeather(@PathVariable("city") String city){
        String result = restTemplate.getForObject(weatherUrl+city, String.class);
        JSONObject jsonObject = JSON.parseObject(result);
        Object date = jsonObject.get("data");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return  jsonObject.getJSONObject("data");
//        return JSON.toJSONString(date);

    }

    @GetMapping("/get3")
    public void get3(HttpServletResponse response) throws IOException {

//        response.setContentType("image/jpeg");

        URL url = new URL("http://pic37.nipic.com/20140113/8800276_184927469000_2.png");
        // 创建链接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(50 * 1000);
        InputStream inStream = conn.getInputStream();
        OutputStream outputStream = response.getOutputStream();
        IOUtils.copy(inStream,outputStream);

        inStream.close();
        outputStream.flush();
        outputStream.close();
    }

    @GetMapping("/get4")
    public void get4(HttpServletResponse response) throws IOException {

        HttpServletRequest request = restTemplate.getForObject("http://127.0.0.1:8080/rest/get3", HttpServletRequest.class);
        ServletOutputStream outputStream = response.getOutputStream();
        ServletInputStream inputStream = request.getInputStream();
        IOUtils.copy(inputStream,outputStream);
    }


    @GetMapping("/get5")
    public void get5(Module module) throws IOException {

        System.out.println(module);
    }


}
